package model;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanTable;

public class Model {
	private BookmarkDAO bookmarkDAO;
	private UserDAO  userDAO;

	public Model(ServletConfig config) throws ServletException {
		try{
			String jdbcDriver = config.getInitParameter("jdbcDriverName");
			String jdbcURL    = config.getInitParameter("jdbcURL");
			String dbUser = config.getInitParameter("dbUsername");
			String dbPassword = config.getInitParameter("dbPassword");
			//System.out.println("Database connection information: " + jdbcDriver + ", " +  jdbcURL + ", "  + dbUser + ", " + dbPassword);
			if(dbUser != null && dbUser.length() > 0 && dbPassword!= null && dbPassword.length() > 0){
				BeanTable.useJDBC(jdbcDriver, jdbcURL, dbUser, dbPassword);
			}else{
				BeanTable.useJDBC(jdbcDriver, jdbcURL);
			}
			userDAO  = new UserDAO();
			bookmarkDAO = new BookmarkDAO();
		}catch(DAOException e) {
			e.printStackTrace();
			throw new ServletException(e);
		}
	}
	
	public BookmarkDAO getBookmarkDAO() { 
		return bookmarkDAO; 
	}
	
	public UserDAO  getUserDAO()  { 
		return userDAO;  
	}
}
